﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;

namespace WarmMeUp.Layouts.WarmMeUp
{
    public partial class WarmMeUpConfig : LayoutsPageBase
    {
        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // We have to initialize le AdditionalUrl with the value contained in the WarmUp custom property
                // First, we look if the custom property exists
                if (SPContext.Current.Site.WebApplication.Properties.ContainsKey(Constants.AdditionalUrls))
                {
                    // OK, the property exists. We can initialize InputFormTextBox
                    AdditionalUrl.Text = SPContext.Current.Site.WebApplication.Properties[Constants.AdditionalUrls] as string;
                }
                else
                {
                    // The property should exists, we create it and save changes
                    bool orginalUnsafe = SPContext.Current.Site.AllowUnsafeUpdates;
                    SPContext.Current.Web.AllowUnsafeUpdates = true;
                    try
                    {
                        SPContext.Current.Site.WebApplication.Properties.Add(Constants.AdditionalUrls, string.Empty);
                        SPContext.Current.Site.WebApplication.Update();
                    }
                    catch (Exception ex)
                    {
                        SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(Constants.TraceKey, TraceSeverity.High, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
                    }
                    finally
                    {
                        SPContext.Current.Web.AllowUnsafeUpdates = orginalUnsafe;
                    }
                }
            }
        }

        /// <summary>
        /// Method called when the user validate modifications
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSetAdditionalUrl_Click(object sender, EventArgs e)
        {
            using (SPLongOperation operation = new SPLongOperation(this))
            {
                operation.Begin();
                // We save changes
                bool orginalUnsafe = SPContext.Current.Site.AllowUnsafeUpdates;
                SPContext.Current.Web.AllowUnsafeUpdates = true;
                try
                {
                    SPContext.Current.Site.WebApplication.Properties[Constants.AdditionalUrls] = AdditionalUrl.Text;
                    SPContext.Current.Site.WebApplication.Update();
                }
                catch (Exception ex)
                {
                    SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(Constants.TraceKey, TraceSeverity.High, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
                }
                finally
                {
                    SPContext.Current.Web.AllowUnsafeUpdates = orginalUnsafe;
                }
                // Redirect to monitoring home page
                operation.End("/monitoring.aspx", SPRedirectFlags.UseSource | SPRedirectFlags.Static, this.Context, null);
            }
        }
    }
}
